package com.bookshopsystem.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.bookshopsystem.mod.Publisher;

public class PublishDaoImp implements PublisherDao {

	private Connection con = null;
	private PreparedStatement stmt = null;
	private String sql = null;
	public PublishDaoImp(Connection con){
		this.con=con;
	}
	@Override
	public List<Publisher> getPublisher(int num) throws Exception{
		// TODO Auto-generated method stub
		if (num==0) {
			sql="select * from publishers";
			stmt = con.prepareStatement(sql);
		}else {
			sql="select * from publishers order by id limit 0,?";
			stmt = con.prepareStatement(sql);
			stmt.setInt(1, num);
		}
		ResultSet rs = stmt.executeQuery();
		List<Publisher> publishers=new ArrayList<Publisher>();
		while (rs.next()) {
			Publisher publisher=new Publisher();
			publisher.setId(rs.getInt(1));
			publisher.setName(rs.getString(2));
			publishers.add(publisher);
		}
		return publishers;
	}

}
